package lntu.cn.cxxd0927;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class RecyclerDemo extends Activity {

    int[] imageList={R.drawable.f1,R.drawable.f2,R.drawable.f3,R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p1,R.drawable.p2,};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_demo);

        //找到Recyclerview
        RecyclerView recyclerView=findViewById(R.id.rv_main);
        //设置布局管理器
        //垂直滚动
        // recyclerView.setLayoutManager(new LinearLayoutManager(this));

        //水平滚动
        //recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));

        //网格效果
        /*GridLayoutManager gridLayoutManager=new GridLayoutManager(this,3);//spanCount表示列数
        recyclerView.setLayoutManager(gridLayoutManager);*/

        //瀑布流效果
        StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(2,LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        //创建适配器对象
        MyAdapter myAdapter=new MyAdapter();
        //为recyclerView添加适配器
        recyclerView.setAdapter(myAdapter);


    }
    class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view=View.inflate(getApplicationContext(),R.layout.item2,null);
            MyViewHolder myViewHolder=new MyViewHolder(view);

            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
                holder.imageView.setImageResource(imageList[position]);
        }

        @Override
        public int getItemCount() {
            return imageList.length;
        }
    }

}
